package com.xyht.sca_s.student_manage_system.modules.personnelManagement.controller;


import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.personnelManagement.entity.req.SmsContractManagementAddReq;
import com.xyht.sca_s.student_manage_system.modules.personnelManagement.entity.req.SmsContractManagementDeletedReq;
import com.xyht.sca_s.student_manage_system.modules.personnelManagement.entity.req.SmsContractManagementUpdateReq;
import com.xyht.sca_s.student_manage_system.modules.personnelManagement.service.SmsContractManagementService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 合同管理 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-09-25
 */
@RestController
@RequestMapping("/personnelManagement/contractManagement")
@SecretBody
public class SmsContractManagementController {

    @Autowired
    private SmsContractManagementService contractManagementService;

    @ApiModelProperty("上传合同管理")
    @PostMapping("/add")
    public ResponseResult addContractManagement(@RequestBody SmsContractManagementAddReq addReq){
        return contractManagementService.addContractManagement(addReq);
    }

    @ApiModelProperty("查看合同管理列表")
    @GetMapping("/list")
    public ResponseResult getContractManagementList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "tchName",required = false) String tchName){
        return contractManagementService.getContractManagementList(pageNum,pageSize,tchName);
    }

    @ApiModelProperty("查看合同管理详情")
    @GetMapping("/detail")
    public ResponseResult getContractManagementDetail(@RequestParam(value = "id") String id){
        return contractManagementService.getContractManagementDetail(id);
    }

    @ApiModelProperty("修改合同管理")
    @PostMapping("/update")
    public ResponseResult updateContractManagement(@RequestBody SmsContractManagementUpdateReq updateReq){
        return contractManagementService.updateContractManagement(updateReq);
    }

    @ApiModelProperty("删除合同管理")
    @PostMapping("/deleted")
    public ResponseResult deletedContractManagement(@RequestBody SmsContractManagementDeletedReq deletedReq){
        return contractManagementService.deletedContractManagement(deletedReq);
    }


    @ApiModelProperty(value = "合同下载")
    @GetMapping("/download")
    public ResponseResult tableDownload(@RequestParam(value = "contractId") String contractId, HttpServletResponse response) {
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = contractManagementService.tableDownload(contractId, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;

    }
}

